new Vue({
  el: '#app',
  data() {
    return {
      fileList: [],
      limit: 1,//上传最大文件数
      form: {
        // Contract_Template_Name: '',
        Contract_Table_Name: '',
        Contract_Table_Type: '',
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
      },
      rules: {
        Contract_Template_Name: [{ required: true, message: '请输入模板名称', trigger: 'blur' }],
      },
      loading: false
    }
  },
  mounted() {

  },
  computed: {
    Data() {
      return JSON.stringify({
        Enterprise_Info_SerialNumber: this.form.Enterprise_Info_SerialNumber,
        // Contract_Template_Name: this.form.Contract_Template_Name,
        Contract_Table_Name: this.form.Contract_Table_Name,
        Contract_Table_Type: this.form.Contract_Table_Type,
        token: $.cookie('token')
      })
    }
  },
  methods: {
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          let { uploadFiles } = _this.$refs.upload
          let form = new FormData()
          let status = true;
          uploadFiles.forEach((item, i) => {
            const size = item.raw.size / 1024 <= 500
            if (!size) {
              this.$message.error(`${item.raw.name}大小超过500KB`)
              status = false
              return
            }
            form.append('file', item.raw)
          })
          if (status) {
            form.append('Data', _this.Data)
            console.log(_this.Data);
            $.ajax({
              headers,
              url: BASE_URL + '/contract/createTable', // 替换为你的上传URL  
              type: 'POST',
              data: form,
              processData: false, // 告诉 jQuery 不要去处理发送的数据  
              contentType: false, // 告诉 jQuery 不要去设置 Content-Type 请求头  
              xhr: function () {
                var xhr = new XMLHttpRequest();
                xhr.upload.onprogress = function (e) {
                  if (e.lengthComputable) {
                    var percentComplete = (e.loaded / e.total) * 100;
                    console.log(percentComplete + '% 上传完成.');
                    // 在这里可以更新 UI 来显示上传进度  
                  }
                };
                return xhr;
              },
              success: function (res) {
                if (res.status == 0) {
                  _this.$message.success("提交成功");
                  window.history.go(-1);
                } else {
                  _this.$message.error("提交失败，请重试！");
                }
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false;
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              error: function (xhr, status, error) {
                _this.$message.error("操作失败，请稍后再试")
              }
            });
          }
        } else {
          console.log("error submit!");
          return false;
        }
      });
    },
    handleRemove(file, fileList) {//移除
      this.fileList = fileList;
    },
    handleExceed(files, fileList) {// 选取文件超过数量提示
      this.$message.warning(`当前限制选择 ${this.limit} 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    handleChange(file, fileList) {//监控上传文件列表
      let existFile = fileList.slice(0, fileList.length - 1).find(f => f.name === file.name);
      if (existFile) {
        this.$message.error('当前文件已经存在!');
        fileList.pop();
      }
      this.fileList = fileList;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
})